<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }"
                >首页</el-breadcrumb-item
            >
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <!-- 搜索和添加 -->
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
                        <el-button
                            slot="append"
                            icon="el-icon-search"
                            @click="getGoodsList"
                        ></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="goAddPage">添加商品</el-button>
                </el-col>
            </el-row>
            <!-- 商品列表 -->
            <el-table :data="goodsList" border stripe>
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="商品名称" prop="goods_name"></el-table-column>
                <el-table-column label="商品价格(元)" prop="goods_price" width="95px"></el-table-column>
                <el-table-column label="商品重量" prop="goods_weight" width="70px"></el-table-column>
                <el-table-column label="创建时间" width="95px">
                    <template slot-scope="scope">
                        {{ scope.row.add_time*1000| dateTIMEFormat }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="130px">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" icon="el-icon-edit" @click="selectList(scope.row.goods_id)"></el-button>
                        <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteList(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                background
				style="margin-top:15px"
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="queryInfo.pagenum"
				:page-sizes="[10, 20, 50]"
				:page-size="queryInfo.pagesize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
			></el-pagination>
        </el-card>
        <el-dialog
                title="修改商品名称"
                :visible.sync="modifygoods"
            >
                <el-form
                    :model="modifyGoodsdata"
                    :rules="addFormGoods"
                    label-width="80px"
                    ref="modifyGoodsRef"
                >
                    <el-form-item label="商品名称" prop="goods_name" >
                        <el-input v-model="modifyGoodsdata.goods_name" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
			        <el-button @click="modifygoods = false">取 消</el-button>
			        <el-button type="primary" @click="modifyGoodsInfo()">确 定</el-button>
			    </div>
            </el-dialog>
    </div>
</template>
<script>
// import {formatDate} from 'src/filters/formatDate.js'
export default {
    data(){
        return {
            queryInfo: {
                query: "",
                pagenum: 1,
                pagesize: 5,
            },
            goodsList:[],
            total:0,
            modifygoods:false,
            addFormGoods:{
                goods_name:[
                    { required: true, message: "请输入商品名称", trigger: "blur" },
                ]
            },
            modifyGoodsdata:{},
        }
    },
    created() {
        this.getGoodsList()
    },
    methods: {
         async getGoodsList() {
            const { data: res } = await this.$http.get("goods", {
                params: this.queryInfo,
            });

            if (res.meta.status != 200) {
                return this.message.error("获取商品列表失败");
            }

            this.goodsList = res.data.goods;
            this.total = res.data.total;

            console.log(this.goodsList);
        },
        // 监听pagesize改变的事件
		handleSizeChange(newSize) {
			this.queryInfo.pagesize = newSize;
			this.getGoodsList();
		},
		// 监听页码值改变的事件
		handleCurrentChange(newPage) {
			this.queryInfo.pagenum = newPage;
			this.getGoodsList();
        },
        async deleteList (id) {
            // 弹框询问用户是否删除数据
            const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err => err)
            // 确认删除 则返回字符串confirm 否则返回cancel
            if (confirmResult !== 'confirm') {
            return this.$message.info('已经取消删除')
            }
            const { data: res } = await this.$http.delete('goods/' + id);
            if (res.meta.status !== 200) {
                return this.$message.error('删除商品失败');
            }
            this.$message.success('已成功删除');
            this.getGoodsList();
        },
        //编辑商品发送请求
        async selectList(id){
            const { data: res } = await this.$http.get(`goods/${id}`)
            if(res.meta.status !== 200){
                return this.$message.error("查询商品失败");
            }
            console.log(res)
            this.modifygoods=true;
            this.modifyGoodsdata=res.data;
        },
        //编辑商品提交请求
        modifyGoodsInfo(){
            this.$refs.modifyGoodsRef.validate(async valid => {
                //判断状态
                if(!valid){
                    return this.$message.error("请输入修改商品信息");
                }
                const { data: res } = await this.$http.put(`goods/${this.modifyGoodsdata.goods_id}`,{
                    goods_name:this.modifyGoodsdata.goods_name,
                })
                console.log(res)
                //判断数据是否请求道
                if (res.meta.status!== 200) {
                    return this.$message.error('修改商品失败');
                }
                // 关闭对话框
                this.modifygoods = false;
                this.getGoodsList();
                this.$message.success('修改商品成功');
            })
        },    
        goAddPage(){
            this.$router.push('/goods/add')
        }    
    },
}
</script>
<style lang="less">
    
</style>